﻿/*****************************************************************************  

 Version: 0.1
 Website: http://www.visuagile.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate;
using Spring.Data.NHibernate.Support;

using VisualAgile.IDAL;
using VisualAgile.Model;
using Spring.Data.NHibernate;
using NHibernate.Engine;

namespace VisualAgile.DAL
{
    public class FeatureDao : BaseDao<Feature>, IFeatureDao
    {
        protected override HibernateTemplate getBaseDAO()
        {
            return HibernateTemplate;
        }

        public IList FindByRelease(string releaseId)
        {
            return HibernateTemplate.Find("from Features where ReleaseID = ?", releaseId);            
        }

        public IList FindByProject(Guid ProjectID)
        {
            IQuery query = Session.CreateQuery("from Feature F, Release R, Project P where F.ReleaseID = R.ReleaseID and R.ProjectID = P.ID and P.ID='" + ProjectID.ToString() + "'");

            //foreach (object obj in (object[])query.List()[0])
            //{
            //    Feature feature = obj as Feature;
            //    if(feature != null
            //}
            IList features = query.List();

            if (features.Count > 0)
            {
                features = ((object[])query.List()[0])[0] as List<Feature>;

                if (features == null)
                {
                    features = new List<Feature>();
                    Feature feature = ((object[])query.List()[0])[0] as Feature;
                    if (feature != null)
                        features.Add(feature);
                }
            }

            return features;
        }
    }
}